package com.hiof.servercommunication.background.job.managers;

import com.hiof.servercommunication.background.job.LiveLocationSendJob;
import com.hiof.servercommunication.background.job.NextTargetLocationJob;
import com.hiof.servercommunication.background.job.TeamMemberLocationJob;
import com.hiof.servercommunication.background.job.TimerJobManager;
import com.hiof.servercommunication.background.job.listener.IListener;
import com.hiof.servercommunication.background.job.listener.ILiveLocationSendListener;
import com.hiof.servercommunication.background.job.listener.INextTargetLocationListener;
import com.hiof.servercommunication.background.job.listener.ITeamMemberLocationListener;

/**
 * Created by IntelliJ IDEA.
 * User: manish
 * Date: 4/1/12
 * Time: 12:12 PM
 * To change this template use File | Settings | File Templates.
 */
public class AthleteBackgroundThreadManager extends AbstractBackgroundThreadManager {

    private static AthleteBackgroundThreadManager athleteBackgroundManager;

    protected LiveLocationSendJob liveLocationSendJob;
    private TeamMemberLocationJob teamMemberLocationJob;
    private NextTargetLocationJob nextTargetLocationJob;
    private TimerJobManager timerJobManager;

    public static AthleteBackgroundThreadManager getInstance(){
        if(null == athleteBackgroundManager){
            athleteBackgroundManager = new AthleteBackgroundThreadManager();
        }
        return athleteBackgroundManager;
    }
    private AthleteBackgroundThreadManager() {
        timerJobManager = TimerJobManager.getInstance();
        teamMemberLocationJob = new TeamMemberLocationJob();
        nextTargetLocationJob = new NextTargetLocationJob();
        liveLocationSendJob = new LiveLocationSendJob();
    }

    @Override
    public void setUp() {
//        TODO uncomment
        timerJobManager.addTimerJob(liveLocationSendJob);
        timerJobManager.addTimerJob(teamMemberLocationJob);
        timerJobManager.addTimerJob(nextTargetLocationJob);
    }

    @Override
    public void registerListener(IListener listener) {
        if(listener instanceof ILiveLocationSendListener){
            liveLocationSendJob.addListener(listener);
        }
        if(listener instanceof ITeamMemberLocationListener){
            teamMemberLocationJob.addListener(listener);
        }
        if(listener instanceof INextTargetLocationListener){
             nextTargetLocationJob.addListener(listener);
        }
    }

    @Override
    public void removeListener(IListener listener) {
        if(listener instanceof ILiveLocationSendListener){
            teamMemberLocationJob.removeListener(listener);
        }
        if(listener instanceof ITeamMemberLocationListener){
            teamMemberLocationJob.removeListener(listener);
        }
        if(listener instanceof INextTargetLocationListener){
            nextTargetLocationJob.removeListener(listener);
        }
    }
}
